/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interfaces;

import Objetos.AltaIncidencia;
import Objetos.SQLconnection;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Gema
 */
public class ListadoIncidencias extends javax.swing.JFrame {

    /**
     * Creates new form ListadoIncidencias
     */
    SQLconnection s = new SQLconnection();
    DefaultTableModel modelo = new DefaultTableModel();
    int id;
    int tipo;

    public ListadoIncidencias() {
        initComponents();


    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(15, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        int fila, columna;
        String f;
        switch (tipo) {
            case 1:
                fila = jTable1.rowAtPoint(evt.getPoint());
                columna = jTable1.columnAtPoint(evt.getPoint());

                if ((fila > -1) && (columna > -1)) {
                    try {

                        f = (String) (modelo.getValueAt(fila, 0));
                        Interfaz_2_1 i = new Interfaz_2_1(f, id + "");
                        this.setVisible(false);
                        i.setVisible(true);
                    } catch (ParseException ex) {
                        Logger.getLogger(ListadoIncidencias.class.getName()).log(Level.SEVERE, null, ex);
                    }


                }
                break;
            case 2:
                fila = jTable1.rowAtPoint(evt.getPoint());
                columna = jTable1.columnAtPoint(evt.getPoint());
                if ((fila > -1) && (columna > -1)) {
                    try {
                        f = (String) (modelo.getValueAt(fila, 0));
                        Interfaz_2_3 i = new Interfaz_2_3(f, id + "");
                        this.setVisible(false);
                        i.setVisible(true);
                    } catch (ParseException ex) {
                        Logger.getLogger(ListadoIncidencias.class.getName()).log(Level.SEVERE, null, ex);
                    }


                }
                break;
        }
    }//GEN-LAST:event_jTable1MouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ListadoIncidencias.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ListadoIncidencias.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ListadoIncidencias.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ListadoIncidencias.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ListadoIncidencias().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    public void llenarTabla(int id) {
        this.id = id;
        tipo = 1;
        modelo.addColumn("Id");
        modelo.addColumn("Descripión");
        //Traemos los datos
        s.connectar();
        AltaIncidencia[] a = s.buscarAltasMaestro(id);
        int contador = 0;
        while (contador < a.length) {

            Object[] fila = new Object[2];
            fila[0] = a[contador].getCod() + "";
            fila[1] = a[contador].getDescripcion();
            modelo.addRow(fila); // Añade una fila al final

            contador++;
        }
        jTable1.setModel(modelo);
        jTable1.repaint();

    }

    void llenarTabla2(int dni) {
        this.id = dni;
        tipo = 2;
        modelo.addColumn("Id");
        modelo.addColumn("Descripión");
        //Traemos los datos
        s.connectar();
        AltaIncidencia[] a = s.buscarAltasMaestroC(id);
        int contador = 0;
        while (contador < a.length) {

            Object[] fila = new Object[2];
            fila[0] = a[contador].getCod() + "";
            fila[1] = a[contador].getDescripcion();
            modelo.addRow(fila); // Añade una fila al final

            contador++;
        }
        jTable1.setModel(modelo);
        jTable1.repaint();

    }
}
